package com.muslimedia.reloadmanager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

import com.muslimedia.reloadmanager.util.FileProcess;

public class Splash extends Activity {
	private final int SPLASH_LENGTH = 1000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				try{
					String dir = Splash.this.getString(R.string.root_folder);
					File f = new File(dir);
					if(!f.exists())f.mkdir();
					String path = dir+"reload_manager.db"; 
					f = new File(path);
					if(!f.exists()){
						InputStream in = getAssets().open("reload_manager.db");
						OutputStream out = new FileOutputStream(path);
						FileProcess.copy(in, out);
					}
					
					Intent mainIntent = new Intent(Splash.this, Home.class);
					Splash.this.startActivity(mainIntent);
					Splash.this.finish();
					
				}catch(Exception e){
					String msg = "Failed to copy database reload manager.";
					Log.e("tag", msg, e);
					Toast.makeText(Splash.this, msg, Toast.LENGTH_LONG).show();
				}
			}
		}, SPLASH_LENGTH);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_splash, menu);
		return true;
	}

}
